package org.example.threadpool;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.utils.Sleeper;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author zhengdp
 * @Description 线程池中的invokeAll方法测试
 * @Date 2022/12/13 22:44
 * @Created by zhengdp
 */
@Slf4j(topic = "c.InvokeAllTest")
public class InvokeAnyTest {
    @SneakyThrows
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        String result = pool.invokeAny(Arrays.asList(() -> {
                    log.info("begin");
                    Sleeper.sleep(1000);
                    log.info("end");
                    return "1";
                },
                () -> {
                    log.info("begin");
                    Sleeper.sleep(500);
                    log.info("end");
                    return "2";
                },
                () -> {
                    log.info("begin");
                    Sleeper.sleep(2000);
                    log.info("end");
                    return "3";
                }));

        log.info("{}",result);

    }
}
